﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

using Microsoft.WindowsMobile;
using Microsoft.WindowsMobile.Status;
using Microsoft.WindowsMobile.PocketOutlook;
using Microsoft.WindowsMobile.PocketOutlook.MessageInterception;

namespace Senthil.Mobile.WhereAmI
{
    public class SMSHandler : INotifier, IInputMessageReceiver, IDisposable
    {
        MessageInterceptor interceptor;
        string currentConfiguredMessage = string.Empty;
        bool enabled = false;

        public SMSHandler()
        {
        }

        public void Enable()
        {
            if (enabled)
                return;

            enabled = true;

            InitializeInterceptor();
        }

        private void InitializeInterceptor()
        {
            if (interceptor == null)
            {
                interceptor = new MessageInterceptor(InterceptionAction.NotifyAndDelete, true);
            }
            else
            {
                interceptor.Dispose();
                interceptor = new MessageInterceptor(InterceptionAction.NotifyAndDelete, true);
            }

            if (currentConfiguredMessage != null)
            {
                interceptor.MessageCondition = new MessageCondition(MessageProperty.Body, MessagePropertyComparisonType.Equal,
                        currentConfiguredMessage, false);
            }

            if (interceptor != null)
                interceptor.MessageReceived += new MessageInterceptorEventHandler(interceptor_MessageReceived);
        }

        public void Disable()
        {
            enabled = false;

            if (interceptor == null)
                return;

            interceptor.MessageReceived -= new MessageInterceptorEventHandler(interceptor_MessageReceived);
            interceptor.Dispose();
            interceptor = null;
        }

        public void ReceiveOnlyMessage(string message)
        {
            currentConfiguredMessage = message;
            InitializeInterceptor();
        }

        void interceptor_MessageReceived(object sender, MessageInterceptorEventArgs e)
        {
            if (InputMessageReceived != null && enabled)
            {
                InputMessageReceived(this, new InputMessageReceivedEventArgs(GetPhoneNumber(e.Message.From.Address), currentConfiguredMessage));
            }
        }

        string GetPhoneNumber(string address)
        {
            string[] arr = address.Split(' ');

            string val = arr[arr.Length - 1];

            if (val.Length < 2)
                return "";

            return val.Substring(1, val.Length - 2);
        }

        #region INotifier Members

        public void Notify(string[] users, NotificationMessage message)
        {
            SmsMessage smsMessage = new SmsMessage();
            
            foreach(string user in users)
                smsMessage.To.Add(new Recipient(user));

            smsMessage.Body = message.ToString();

            smsMessage.Send();
        }

        #endregion

        #region IInputMessageReceiver Members

        public event EventHandler<InputMessageReceivedEventArgs> InputMessageReceived;

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            Disable();
            interceptor.Dispose();
        }

        #endregion
    }
}
